 net set ado "Z:\stata"
 adopath + "Z:\stata"
 ssc install mimrgns
use "Data/imp.dta",replace
mi xtset after yearx
mi convert flong, clear

********************************************
* Prediction
********************************************
use "Data/imp.dta",replace

mi xtset after yearx
mi convert flong, clear

local econ2 "onsenchi_w onsenchi_b farming_w farming_b logpop_w logpop_b income_w income_b health_w health_b"
local soci2 "turnout_w turnout_b"
local control "logpot sumexlagdm_b sumexlagdm_w fourdiv3 fourdiv4 nptypex3_w nptypex3_b changebde_w changebde_b i.yearx" 

eststo: mi estimate, saving(Results/miestfile,replace) esample(esample) : qui xtlogit exdm_m `econ2' `soci2' `control', re nolog
mi predictnl model2 = predict(pr) using Results/miestfile

mimrgns yearx using Results/miestfile, esample(esample) predict(pr) at(onsenchi_b=(0)) atmeans
matrix A = r(table) 
mimrgns yearx using Results/miestfile, esample(esample) predict(pr) at(onsenchi_b=(15)) atmeans
matrix B = r(table) 
mimrgns yearx using Results/miestfile, esample(esample) predict(pr) at(onsenchi_b=(30)) atmeans
matrix C = r(table) 
mimrgns yearx using Results/miestfile, esample(esample) predict(pr) at(onsenchi_b=(45)) atmeans
matrix D = r(table) 

matrix G=A\B\C\D
esttab matrix(G) using Results/Prediction_OnsenBtw.csv,replace plain

********************************************
* Prediction farming
********************************************
use "Data/imp.dta",replace

mi convert flong, clear

local econ2 "onsenchi_w onsenchi_b farming_w farming_b logpop_w logpop_b income_w income_b health_w health_b"
local soci2 "turnout_w turnout_b"
local control "logpot sumexlagdm_b sumexlagdm_w fourdiv3 fourdiv4 nptypex3_w nptypex3_b changebde_w changebde_b i.yearx" 

eststo: mi estimate, saving(Results/miestfile,replace) esample(esample) : qui xtlogit exdm_m `econ2' `soci2' `control', re nolog

mimrgns using Results/miestfile, esample(esample) predict(pr)  at(farming_w=(28 14 0) yearx=(0)) atmeans 
matrix A = r(table) 
mimrgns using Results/miestfile, esample(esample) predict(pr)  at(farming_w=(20 10 0) yearx=(1)) atmeans 
matrix B = r(table) 
mimrgns using Results/miestfile, esample(esample) predict(pr) at(farming_w=(12 6 0) yearx=(2)) atmeans  
matrix C = r(table)
mimrgns using Results/miestfile, esample(esample) predict(pr) at(farming_w=(4 2 0) yearx=(3)) atmeans  
matrix D = r(table)
mimrgns using Results/miestfile, esample(esample) predict(pr) at(farming_w=(-4 -2 0) yearx=(4)) atmeans  
matrix E = r(table)
mimrgns using Results/miestfile, esample(esample) predict(pr) at(farming_w=(-12 -6 0) yearx=(5)) atmeans  
matrix F = r(table)
mimrgns using Results/miestfile, esample(esample) predict(pr)  at(farming_w=(-20 -10 0) yearx=(6)) atmeans  
matrix G = r(table)
mimrgns using Results/miestfile, esample(esample) predict(pr) at(farming_w=(-28 -14 0) yearx=(7)) atmeans  
matrix H = r(table)
 
matrix M=A\B\C\D\E\F\G\H

esttab matrix(M) using Results/Prediction_FarmingWth.csv,replace plain

********************************************
* Table A7
********************************************

use "Data/imp.dta",replace
mi xtset after yearx
mi convert flong, clear

local econ2 "onsenchi_w onsenchi_b farming_w farming_b logpop_w logpop_b income_w income_b health_w health_b"
local soci2 "turnout_w turnout_b"
local control "logpot sumexlagdm_b sumexlagdm_w fourdiv3 fourdiv4 nptypex3_w nptypex3_b changebde_w changebde_b i.yearx" 

eststo: mi estimate, saving(Results/miestfile,replace) esample(esample) : qui xtlogit exdm_m `econ2' `soci2' `control', re nolog
mi predictnl model2 = predict(pr) using Results/miestfile

gen obama = 1 if after == 423670
replace obama = 0 if obama == .
keep year obama model2
gen freq = 1

collapse (mean) model2 freq, by(year obama)
drop freq

export excel using "Results/A7[Obama].xls", firstrow(variables) replace

erase Results/miestfile.ster

